package org.advanced.extend;

/**
 * @author John.zhang
 * @version Id: Interrupt, v 1.0 2020/12/7 16:28 ZhiYong Exp $
 * JVM层面的中断
 */
public class Interrupt {

    private static int i;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            //第一种：正常任务中断处理
            while (!Thread.currentThread().isInterrupted()) {
                i++;
            }
            //第二种：阻塞任务中断处理，通过抛异常的方式中断线程，线程被中断的前提条件是必须是run结束
            try {
                Thread.sleep(10000);
                Interrupt.class.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //线程复位，对于无法中断的请求无法中断进行复位
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("before");
                    //中断线程复位
                    Thread.interrupted();
                    System.out.println("after");
                }
            }
        });
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }

}
